﻿var mysql = require('mysql');
var SqliteDB = require('./sqlite.js').SqliteDB;
var config={}

config.mysql={
    IP:'192.168.1.38',
    DB:"bes",
    USER:"root",
    PASSWD:"123456",
    ACCOUNT_DB:"ACCOUNTS"
}

config.link={
    INDEX:"/",
    ORDER_MANAGER:"/",
    GOODS_MANAGER:"/goods_manager",
    USERS:"/users",
    SHOP:"/shop"
}
config.option_heads=[
"编号",
"适用车型",
"配件名称",
"产地",
"库存状态",
"库存量",
"有优惠单位",
"进价",
"零件号",
"物料号",
"条码号",
"上次进货日期",
"图片列表",
]
config.option_heads_client=[
    "编号",
    "适用车型",
    "配件名称",
    "产地",
    "库存状态",
    "库存量",
    "有优惠单位",
    "进价",
    "零件号",
    "物料号",
    "条码号",
    "上次进货日期",
    "购买数量",
    "图片列表",
    "操作",
    ]
config.sqlite = {
    file:"goods.db"
}
var links_client=[
    {
        link:config.link.ORDER_MANAGER,
        name:"订单管理"
    },
    {
        link:config.link.SHOP,
        name:"购物车"
    }
]

var links_manager=[
    {
        link:config.link.GOODS_MANAGER,
        name:"库存管理"
    },
    {
        link:config.link.USERS,
        name:"客户管理"
    }
]
config.linkmenu = links_manager;
config.linkmenu_client = links_client;

config.onlineFile = "onlime_num";

config.aync_each_arr = function(arr,each_callback,all_back)
{
    (function next(i, len, callback) {
        if (i < len) {
            var item = arr[i];

            nextcall = function() {
                next(i + 1, len, callback);
            };

            each_callback( item,nextcall);

        } else {
            callback();
        }
    }(0, arr.length, function () {
        // All array items have processed.
        all_back();
    }));
};

config.checkUser = function(TABLENAME,WHERE,callback){
    var connection = mysql.createConnection({
        host:config.mysql.IP,
        user:config.mysql.USER,
        password:config.mysql.PASSWD,
        database:config.mysql.ACCOUNT_DB
    });
    connection.connect();
    var sql = 'SELECT * FROM '+TABLENAME + " WHERE " + WHERE;
    console.log(sql);
    connection.query(sql,function(err,rows,fields){
        if (err) throw err;
        callback(rows);
    });
    connection.end();
};
config.updateUserVer = function(TABLENAME,WHERE,callback){
    var connection = mysql.createConnection({
        host:config.mysql.IP,
        user:config.mysql.USER,
        password:config.mysql.PASSWD,
        database:config.mysql.ACCOUNT_DB
    });
    connection.connect();
    var sql = 'UPDATE `'+TABLENAME + "` SET STATE=1"+" WHERE " + WHERE;
    console.log(sql);
    connection.query(sql,function(err,res){
        if (err) throw err;
        callback(res);
    });
    connection.end();
};


config.checkDBSqlite3 = function(db){
    db.createTable("CREATE TABLE IF NOT EXISTS USER_DB(NAME CHAR(255),PASSWD CHAR(255));");
    db.createTable("CREATE TABLE IF NOT EXISTS GOODS_DB(\
      UNIQUE_TAG integer PRIMARY KEY autoincrement,\
      SHIYONGCHEXING CHAR(255),\
      PEIJIANMINGCHENG CHAR(255),\
      CHANDI CHAR(255),\
      KUNCUNZHUANGTAI CHAR(255),\
      KUNCUNLIANG CHAR(255),\
      YOUHUIDANWEI CHAR(255),\
      JINJIA CHAR(255),\
      LINGJIANHAO CHAR(255),\
      WULIAOHAO CHAR(255),\
      TIAOMAHAO CHAR(255),\
      JINHUOSHIJIAN CHAR(255),\
      IMAGES TEXT\
    );");

    db.createTable("CREATE TABLE IF NOT EXISTS ORDER_DB(\
        ORDER_ID integer PRIMARY KEY autoincrement,\
        USERNAME CHAR(255),\
        GOODS_ID integer,\
        BUY_TIME CHAR(255),\
        BUY_NUMBER integer,\
        ORDER_STATE integer,\
        DATA TEXT\
      );");
}

config.checkUserSqlite3 = function(name,callback) {
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    sqliteDB.queryData("SELECT * FROM USER_DB where NAME=\""+name+"\"",function(objects){
        if (objects.length){
            callback(true);
        }
        else {
            callback(false);
        }
    });
}

config.addUserSqlite3 = function(name,passwd,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    sqliteDB.insertData("INSERT INTO USER_DB(NAME,PASSWD) VALUES(?,?)",[[name,passwd]],function(){
        callback();
    });

}

config.checkUserAndPasswdSqlite3 = function(name,passwd,callback) {
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    sqliteDB.queryData("SELECT * FROM USER_DB where NAME=\""+name+"\"",function(objects){
        if (objects.length){
            if (objects[0].PASSWD == passwd)
                callback(true);
            else 
                callback(false);
        }
        else {
            callback(false);
        }
    });
}

config.selectAllUserSqlite3 = function(callback) {
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);
    sqliteDB.queryData("SELECT * FROM USER_DB",function(objects){
        callback(objects);
    });
}

config.addGoodsSqlite3 = function(object,callback){
      
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    sqliteDB.insertData("INSERT INTO GOODS_DB(\
        SHIYONGCHEXING,\
        PEIJIANMINGCHENG,\
        CHANDI  ,\
        KUNCUNZHUANGTAI  ,\
        KUNCUNLIANG  ,\
        YOUHUIDANWEI  ,\
        JINJIA ,\
        LINGJIANHAO  ,\
        WULIAOHAO  ,\
        TIAOMAHAO  ,\
        JINHUOSHIJIAN  ,\
        IMAGES\
    ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)",[[
        ""+object.SHIYONGCHEXING,
        ""+object.PEIJIANMINGCHENG,
        ""+object.CHANDI  ,
        ""+object.KUNCUNZHUANGTAI  ,
        ""+object.KUNCUNLIANG  ,
        ""+object.YOUHUIDANWEI  ,
        ""+object.JINJIA ,
        ""+object.LINGJIANHAO  ,
        ""+object.WULIAOHAO  ,
        ""+object.TIAOMAHAO  ,
        ""+object.JINHUOSHIJIAN  ,
        ""+object.IMAGES]],function(){
            callback();
        });
   
}

config.updateGoodsSqlite3 = function(object,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    var updateSql = "update GOODS_DB set SHIYONGCHEXING =" + object.SHIYONGCHEXING
     +",PEIJIANMINGCHENG='" + object.PEIJIANMINGCHENG+"'"
     + ",CHANDI='" +object.CHANDI+"'"
     + ",KUNCUNZHUANGTAI='" +object.KUNCUNZHUANGTAI+"'"
     + ",KUNCUNLIANG='" +object.KUNCUNLIANG+"'"
     + ",YOUHUIDANWEI='" +object.YOUHUIDANWEI+"'"
     + ",JINJIA='" +object.JINJIA+"'"
     + ",LINGJIANHAO='" +object.LINGJIANHAO+"'"
     + ",WULIAOHAO='" +object.WULIAOHAO+"'"
     + ",TIAOMAHAO='" +object.TIAOMAHAO+"'"
     + ",JINHUOSHIJIAN='" +object.JINHUOSHIJIAN+"'";
     if (object.IMAGES != "" && object.IMAGES != "undefined")
        updateSql += ",IMAGES='" +object.IMAGES+"'";
     updateSql += " where UNIQUE_TAG=" + object.UNIQUE_TAG;
 
    sqliteDB.executeSql(updateSql,function(){
        callback()}
    );
}

config.selectAllGoodsSqlite3 = function(callback) {
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);
    sqliteDB.queryData("SELECT * FROM GOODS_DB",function(objects){
        callback(objects);
    });
}

config.selectOrderByUserNameSqite3 = function(name,callback) {
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);
    sqliteDB.queryData("SELECT * FROM ORDER_DB WHERE USERNAME='"+name+"'",function(objects){
        callback(objects);
        console.log(objects);
    });
}


config.addOrderSqlite3 = function(object,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);
    sqliteDB.insertData("INSERT INTO ORDER_DB(\
        USERNAME,\
        GOODS_ID,\
        BUY_TIME  ,\
        BUY_NUMBER  ,\
        ORDER_STATE ,\
        DATA \
    ) VALUES(?,?,?,?,?,?)",[[
        ""+object.USERNAME,
        ""+object.GOODS_ID,
        ""+object.BUY_TIME  ,
        ""+object.BUY_NUMBER  ,
        ""+object.ORDER_STATE ,
        ""+object.DATA]],function(){
            callback();
        });
}

config.updateOrderNumberSqlite3 = function(orderid,object,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    var updateSql = "update ORDER_DB set BUY_NUMBER =" + object.BUY_NUMBER;
     updateSql += " where ORDER_ID=" + orderid;
 
    sqliteDB.executeSql(updateSql,function(){
        callback()}
    );
}

config.updateOrderStateSqlite3 = function(orderid,object,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    var updateSql = "update ORDER_DB set ORDER_STATE =" + object.ORDER_STATE;
     updateSql += " where ORDER_ID=" + orderid;
 
    sqliteDB.executeSql(updateSql,function(){
        callback()}
    );
}

config.deleteOrderSqlite3 = function(orderid,object,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);

    var updateSql = "delete FROM ORDER_DB ";
     updateSql += " where ORDER_ID=" + orderid;
 
    sqliteDB.executeSql(updateSql,function(){
        callback()}
    );
}
config.selectOrderByID = function(orderid,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);
    sqliteDB.queryData("SELECT * FROM ORDER_DB WHERE ORDER_ID=" + orderid,function(objects){
        if (objects.length)
            callback(objects[0]);
        else
            callback(null);
    });
}
config.selectGoodsSqlite3 = function(condition,callback){
    var sqliteDB = new SqliteDB(config.sqlite.file);

    config.checkDBSqlite3(sqliteDB);
    sqliteDB.queryData("SELECT * FROM GOODS_DB WHERE " + condition,function(objects){
        callback(objects);
    });
}


module.exports = config;

